package com.geeke.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.geeke.common.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.data.Page;
import com.geeke.common.data.SearchParams;
import com.geeke.sys.entity.User;
import com.geeke.sys.service.UserService;
import com.geeke.utils.ResultUtil;
import com.geeke.utils.StringUtils;

/**
 * 用户表 Controller
 */
@RestController
@RequestMapping(value = "/sys/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        User entity = userService.get(id);

        // 不返回密码
        entity.setLoginPassword("");

        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        Page<User> result = userService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );

        // 不返回密码
        if (result.getRows() != null) {
            for (User entity : result.getRows()) {
                entity.setLoginPassword("");
            }
        }

        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<User> result = userService.listAll(searchParams.getParams(), searchParams.getOrderby());

        // 不返回密码
        if (result != null) {
            for (User entity : result) {
                entity.setLoginPassword("");
            }
        }

        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "save")
    @RequiresPermissions(value={"user:create","user:update"}, logical= Logical.OR)
    public ResponseEntity<JSONObject> save(@RequestBody User user) {
        String id = userService.save(user).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    @PostMapping(value = "delete")
    @RequiresPermissions("user:delete")
    public ResponseEntity<JSONObject> delete(@RequestBody User user) {
        int rows = userService.delete(user);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    @PostMapping(value = "bulkInsert")
    @RequiresPermissions("user:create")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody List<User> users) {
        List<String> ids = userService.bulkInsert(users);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkUpdate")
    @RequiresPermissions("user:update")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody List<User> users) {
        List<String> ids = userService.bulkUpdate(users);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkDelete")
    @RequiresPermissions("user:delete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody List<User> users) {
        int rows = userService.bulkDelete(users);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    // 修改加密字段

    /**
     * 修改 密码
     */
    @PutMapping("/{id}/loginPassword")
    public ResponseEntity<JSONObject> changeLoginPassword(@PathVariable("id") String id, String password) {
        int rows = 0;
        if (!StringUtils.isBlank(id)) {
            rows = userService.changeLoginPassword(id, password);
        }
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    /**
     * 激活当前用户，把is_active字段置为'1'
     * @return
     */
    @GetMapping("/activeCurrentUser")
    public ResponseEntity<JSONObject> activeCurrentUser() {
        int rows = userService.activeCurrentUser();
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

}
